#!/bin/sh

# if app not exist
if [ ! -f /bin/wscd ]; then
    exit 0
fi

# wait to start web and run from goahead code
. /etc/scripts/web_wait.sh
web_wait

# get params
. /etc/scripts/global.sh

LOG="logger -t wscd"

start() {
    get_param
    if [ "$WscModeOption" != "" ] || [ "$WscModeOption" != "0" ]; then
      $LOG "Starting WPS"
	ip route replace 239.255.255.250 dev br0 > /dev/null 2>&1
	if [ "$OperationMode" = "0" ] || [ "$ApCliBridgeOnly" = "1" ]; then
	    wscd -m 1 -a $lan_ipaddr -i ra0 -D &
	else
	    wscd -m 1 -i ra0 -D &
	fi
    fi
}

stop() {
 $LOG "Stopping WPS"
    killall -q wscd
    killall -q -SIGKILL wscd
    route delete 239.255.255.250 > /dev/null 2>&1
}

get_param() {
    eval `nvram_buf_get 2860 WscModeOption lan_ipaddr`
}

case "$1" in
        start)
            start
            ;;

        stop)
            stop
            ;;

        restart)
            stop
            start
            ;;

        *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1
esac
